package org.aphelie.torg.ruler.internal.core.text.character;

import org.aphelie.torg.ruler.core.character.ICharacterSketch;
import org.aphelie.torg.ruler.internal.core.text.IDocumentAttributeNode;
import org.aphelie.torg.ruler.internal.core.text.IDocumentElementNode;
import org.eclipse.core.runtime.CoreException;


public class CharacterSketchNode extends CharacterObjectNode implements ICharacterSketch {

    private static final long serialVersionUID = 1L;
    
    public String getAge() {
        System.err.println("CharacterSketchNode.getAge() " + write());
        String age = getXMLAttributeValue(K_AGE);
        //return (type != null && type.equals("resource")) ? ICharacterLibrary.RESOURCE : ICharacterLibrary.CODE; //$NON-NLS-1$
        return age;
    }
    
    public void setAge(String value) throws CoreException {
        setXMLAttribute(K_AGE, value);
    }
    
    public String getName() {
        return getXMLAttributeValue(K_NAME);
    }

    public void setName(String name) throws CoreException {
        setXMLAttribute(K_NAME, name);
    }

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.ICharacterLibrary#getContentFilters()
     */
    /*
    public String[] getContentFilters() {
        IDocumentElementNode[] children = getChildNodes();
        ArrayList result = new ArrayList();
        for (int i = 0; i < children.length; i++) {
            CharacterObjectNode node = (CharacterObjectNode) children[i];
            if (node.getName().equals(P_EXPORTED)) {
                String name = children[i].getXMLAttributeValue(K_NAME);
                if (name != null && !name.equals("*")) { //$NON-NLS-1$
                    int index = name.indexOf(".*"); //$NON-NLS-1$
                    if (index != -1)
                        name = name.substring(0, index);
                    result.add(name);
                }
            }
        }
        return (String[]) result.toArray(new String[result.size()]);
    }
    */

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.ICharacterLibrary#getPackages()
     */
    public String[] getPackages() {
        return new String[0];
    }

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.ICharacterLibrary#isExported()
     */
    /*
    public boolean isExported() {
        IDocumentElementNode[] children = getChildNodes();
        for (int i = 0; i < children.length; i++) {
            CharacterObjectNode node = (CharacterObjectNode) children[i];
            if (node.getName().equals(P_EXPORTED))
                return true;
        }
        return false;
    }
    */

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.ICharacterLibrary#isFullyExported()
     */
    /*
    public boolean isFullyExported() {
        IDocumentElementNode[] children = getChildNodes();
        for (int i = 0; i < children.length; i++) {
            CharacterObjectNode node = (CharacterObjectNode) children[i];
            if (node.getName().equals(P_EXPORTED)) {
                String name = children[i].getXMLAttributeValue(K_NAME);
                if (name != null && name.equals("*")) //$NON-NLS-1$
                    return true;
            }
        }
        return false;
    }
    */

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.ICharacterLibrary#getType()
     */
    /*
    public String getType() {
        String type = getXMLAttributeValue(P_TYPE);
        return (type != null && type.equals("resource")) ? ICharacterLibrary.RESOURCE : ICharacterLibrary.CODE; //$NON-NLS-1$
    }
    */

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.ICharacterLibrary#setContentFilters(java.lang.String[])
    public void setContentFilters(String[] filters) throws CoreException {
    }
     */
    
    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.ICharacterLibrary#addContentFilter(java.lang.String)
    public void addContentFilter(String filter) throws CoreException {
        CharacterElementNode node = new CharacterElementNode();
        node.setXMLTagName(P_EXPORTED);
        node.setParentNode(this);
        node.setModel(getModel());
        node.setXMLAttribute(K_NAME, "*".equals(filter) || filter.endsWith(".*") ? filter : filter + ".*"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        addContentFilter(node);
    }
     */
    /*
    public void addContentFilter(CharacterElementNode node) {
        addChildNode(node);
        if (isInTheModel()) {
            node.setInTheModel(true);
            fireStructureChanged(node, IModelChangedEvent.INSERT);
        }
    }
    */

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.ICharacterLibrary#removeContentFilter(java.lang.String)
    public void removeContentFilter(String filter) throws CoreException {
        if (!filter.endsWith(".*")) //$NON-NLS-1$
            filter += ".*"; //$NON-NLS-1$
        IDocumentElementNode[] children = getChildNodes();
        for (int i = 0; i < children.length; i++) {
            if (children[i].getXMLTagName().equals(P_EXPORTED) && filter.equals(children[i].getXMLAttributeValue(K_NAME))) {
                removeContentFilter((CharacterElementNode) children[i]);
            }
        }
    }
     */
    /*
    public void removeContentFilter(CharacterElementNode node) {
        removeChildNode(node);
        if (isInTheModel()) {
            node.setInTheModel(false);
            fireStructureChanged(node, IModelChangedEvent.REMOVE);
        }
    }
    */

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.ICharacterLibrary#setPackages(java.lang.String[])
    public void setPackages(String[] packages) throws CoreException {
    }
     */
    
    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.ICharacterLibrary#setExported(boolean)
    public void setExported(boolean exported) throws CoreException {
        IDocumentElementNode[] children = getChildNodes();
        boolean alreadyExported = false;
        for (int i = 0; i < children.length; i++) {
            if (children[i].getXMLTagName().equals(P_EXPORTED)) {
                if (!"*".equals(children[i].getXMLAttributeValue(K_NAME))) { //$NON-NLS-1$
                    removeContentFilter((CharacterElementNode) children[i]);
                } else {
                    alreadyExported = true;
                    if (!exported) {
                        removeContentFilter((CharacterElementNode) children[i]);
                    }
                }
            }
        }
        if (exported && !alreadyExported) {
            addContentFilter("*"); //$NON-NLS-1$
        }
    }
     */
    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.ICharacterLibrary#setType(java.lang.String)
     */


    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.ICharacterObject#getName()
     */


    /* (non-Javadoc)
     * @see org.eclipse.pde.internal.ui.model.plugin.CharacterObjectNode#write()
     */
    public String write(boolean indent) {
        String sep = getLineDelimiter();
        StringBuffer buffer = new StringBuffer();
        if (indent)
            buffer.append(getIndent());

        IDocumentElementNode[] children = getChildNodes();
        if (children.length > 0) {
            buffer.append(writeShallow(false) + sep);
            for (int i = 0; i < children.length; i++) {
                children[i].setLineIndent(getLineIndent() + 3);
                buffer.append(children[i].write(true) + sep);
            }
            buffer.append(getIndent() + "</" + getXMLTagName() + ">"); //$NON-NLS-1$ //$NON-NLS-2$
        } else {
            buffer.append(writeShallow(true));
        }
        return buffer.toString();
    }

    /* (non-Javadoc)
     * @see org.eclipse.pde.internal.ui.model.plugin.CharacterObjectNode#writeShallow(boolean)
     */
    public String writeShallow(boolean terminate) {
        StringBuffer buffer = new StringBuffer("<" + getXMLTagName()); //$NON-NLS-1$

        IDocumentAttributeNode[] attrs = getNodeAttributes();
        for (int i = 0; i < attrs.length; i++) {
            appendAttribute(buffer, attrs[i].getAttributeName());
        }
        if (terminate)
            buffer.append("/"); //$NON-NLS-1$
        buffer.append(">"); //$NON-NLS-1$
        return buffer.toString();
    }

    public String toString() {
        return getName();
    }

}
